package com.shuoyue.ycgk.ui.mine.order;

import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.Order;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;

import java.util.List;

import butterknife.BindView;

public class FragmentOrder extends BaseMvpFragment<OrderContract.Presenter>
        implements OrderContract.View {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;

    String title = "";
    OrderAdapter orderAdapter;
    int type = 0;
    int receiptStatus = 0;

    public static FragmentOrder getInstance(int type, int receiptStatus, String title) {
        FragmentOrder fragmentOrder = new FragmentOrder();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putInt("receiptStatus", receiptStatus);
        fragmentOrder.setArguments(bundle);
        fragmentOrder.title = title;
        return fragmentOrder;
    }

    @Override
    public void initParams(Bundle bundle) {
        type = bundle.getInt("type");
        receiptStatus = bundle.getInt("receiptStatus");

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sigle_list;
    }

    @Override
    protected void initView() {
        orderAdapter = new OrderAdapter(null);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.addItemDecoration(new SpacesItemDecoration(16, false));
        recyclerView.setAdapter(orderAdapter);
        registEmptyView(orderAdapter);
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getOrderList(receiptStatus == -1 ? null : receiptStatus, type == -1 ? null : type);
            refreshLayout.finishRefresh();
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            refreshLayout.finishLoadMore();
        });
        orderAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            Order order = (Order) adapter.getItem(position);
//            startActivity(new Intent(mContext, OrderInfoActivity.class));
            OrderInfoActivity.start(mContext, order.getId());
        });
    }

    @Override
    protected void initData() {
        super.initData();

//        orderAdapter.addData(getDebugOrder());
        mPresenter = new OrderContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getOrderList(receiptStatus == -1 ? null : receiptStatus, type == -1 ? null : type);

    }

    @Override
    public String getTitle() {
        return title;
    }


    @Override
    public void setOrderList(List<Order> baseTypes) {
        orderAdapter.resetData(baseTypes);
    }

}
